package com.dongdongshop.controller;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.vo.BrandVO;
import com.dongdongshop.domain.vo.ItemCatVO;
import com.dongdongshop.domain.vo.TypeTemplateVO;
import com.dongdongshop.service.ItemCatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cat")
@CrossOrigin  //跨域
@Api("运营商后台管理——商品分类管理")
@RefreshScope   //自动监听配置文件
@Slf4j
public class ItemCatController {

    @Autowired
    private ItemCatService catService;

    //根据父类id查询
    @PostMapping("catList")
    @ApiOperation("根据父类id查询")
    @ApiImplicitParam(name = "parentId", value = "父id", required = false, paramType = "query")
    public Result catList(@RequestParam Long parentId) {
        List<ItemCatVO> voList = catService.catList(parentId);
        return Result.OK().setData(voList);
    }

    //增加
    @PostMapping("add")
    @ApiOperation(value = "增加商品分类")
    @ApiImplicitParam(name = "itemCat", value = "商品分类类", required = false, paramType = "body")
    public Result addItemCat(@RequestBody ItemCatVO itemCat) {
        catService.addItemCat(itemCat);
        return Result.OK();
    }

    //根据id查询数据/回显
    @PostMapping("selectById")
    @ApiOperation("根据id查询/回显")
    @ApiImplicitParam(name = "id", value = "主键id", required = false, paramType = "query")
    public Result selectById(@RequestParam Long id) {
        ItemCatVO vo = catService.selectById(id);
        return Result.OK().setData(vo);
    }

    //修改
    @PutMapping("update")
    @ApiOperation(value = "修改商品分类")
    @ApiImplicitParam(name = "itemCat", value = "商品分类类", required = false, paramType = "body")
    public Result updateItemCat(@RequestBody ItemCatVO itemCat) {
        catService.updateItemCat(itemCat);
        return Result.OK();
    }

    //批量删除
    @DeleteMapping("delete")
    @ApiOperation(value = "修改商品分类")
    @ApiImplicitParam(name = "ids", value = "主键id的list集合", required = false, paramType = "body")
    public Result delete(@RequestBody List<Long> ids) {
        catService.delete(ids);
        return Result.OK();
    }

    //根据id查询对应的type_id,并查询对应的品牌
    @PostMapping("selectBrandId")
    public Result selectBrandId(@RequestParam Long id) {
        String typeId = catService.selectBrandId(id);
        return Result.OK().setData(typeId);
    }

    //通过分类id查询对应的模板
    @GetMapping("getTemplateById")
    public Result getTemplateById(@RequestParam Long id) {
        TypeTemplateVO typeTemplateVO = catService.getTemplateById(id);
        return Result.OK().setData(typeTemplateVO);
    }
}
